#!/usr/bin/env bash
source $HOME/.owl4ce_var

noterr() { echo "Install iproute2/wireless-tools!"; exit 1; }
command -v {"ip","iwgetid"} > /dev/null 2>&1 || noterr

wifi=""
wifi_noip=""
wifi_disabled=""
lan=""
lan_noip=""
void=""

if iwgetid "$INT_WIFI" &> /dev/null; then
    if [[ "$(iwgetid "$INT_WIFI" | awk -F'"' '{print $2}')" != "" ]]; then
        if ip addr show "$INT_WIFI" | grep 'inet' &> /dev/null; then
            status="$(iwgetid "$INT_WIFI" --raw)"
            icon="$(echo "$wifi")"
        else
            status="$(echo "No IP address ("$INT_WIFI")")"
            icon="$(echo "$wifi_noip")"
        fi
    else
        status="$(echo "Disconnected")"
        icon="$(echo "$wifi_disabled")"
    fi
elif ip link show "$INT_ETH" &> /dev/null; then
    if ip addr show "$INT_ETH" | grep 'inet' &> /dev/null; then
        status="$(echo "$(ip addr show "$INT_ETH" | grep 'inet' | awk -F'inet ' '{print $2}' | awk -F'/' '{print $1}' | head -n1) ("$INT_ETH")")"
        icon="$(echo "$lan")"
    else
        status="$(echo "No IP address ("$INT_ETH")")"
        icon="$(echo "$lan_noip")"
    fi
else
    status="$(echo "Network interface not found ("$INT_ETH";"$INT_WIFI")")"
    icon="$(echo "$void")"
fi

case $1 in
    status) echo "$status"
    ;;
    icon)   echo "$icon"
    ;;
esac
